home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Development
/
Source
/
DBL Pascal Library
/
Misc
/
ShortFract
next >
Wrap
Text File
|
1991-04-30
|
1KB
|
61 lines
unit ShortFract;
{Provide a short fraction type (for cases where the precision of the Fract type is not needed),}
{and conversions between ShortFract and Fract types. A ShortFract can have values ranging}
{from -2 to 2-(2^14), inclusive. ShortFracs obey twos complement arithmetic, and can be}
{added, subtracted, negated, and compared. Use the Fract routines on converted ShortFract}
{values for multiplication, division, and trig functions. See IM IV-63 for details on the Fract type.}
interface
const
SF_Pos1 = $4000;
SF_Neg1 = $C000;
SF_PosHalf = $2000;
SF_NegHalf = $E000;
type
ShortFract = Integer;
function Fract2ShortFract (f: Fract): ShortFract;
function ShortFract2Fract (s: ShortFract): Fract;
implementation
type
FractParts = record
hi, lo: Integer;
end;
FractConvert = record
case Integer of
1: (
f: Fract;
);
2: (
s: ShortFract;
);
3: (
p: FractParts;
);
end;
function Fract2ShortFract (f: Fract): ShortFract;
var
result: ShortFract;
begin
result := FractConvert(f).s;
if FractConvert(f).p.lo < 0 then {round if needed}
result := result + 1;
Fract2ShortFract := result;
end;
function ShortFract2Fract (s: ShortFract): Fract;
var
result: Fract;
begin
FractConvert(result).s := s;
FractConvert(result).p.lo := 0;
ShortFract2Fract := result;
end;
end.